package com.cssw.fyzb.core.dao.video;

import com.cssw.fyzb.core.model.dodb.CmfVideoComment;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YangYiQun
 */
@Repository
public class VideoCommentDaoImpl implements VideoCommentDao {

    @Override
    public CmfVideoComment findById(Long commentId) {
        return CmfVideoComment.dao.findById(commentId);
    }

    @Override
    public Page<CmfVideoComment> pageByComment(Long videoId, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("videoId", videoId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_video_comment t");
        sqlParse.addSQL(" WHERE video_id = :videoId");
        sqlParse.addSQL(" AND is_show = '1'");
        sqlParse.addSQL(" AND type = '1'");
        sqlParse.addSQL(" ORDER BY t.comment_id desc ");
        return CmfVideoComment.dao.paginate(pageNumber, pageSize, sqlParse);

    }

    @Override
    public Page<CmfVideoComment> pageByReply(Long commentId, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("commentId", commentId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_video_comment t");
        sqlParse.addSQL(" WHERE parent_comment_id = :commentId");
        sqlParse.addSQL(" AND is_show = '1'");
        sqlParse.addSQL(" ORDER BY t.comment_id desc ");
        return CmfVideoComment.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfVideoComment> pageByUserId(Long userId, int pageNumber, int pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("SELECT * FROM cmf_video_comment t");
        sqlParse.addSQL(" WHERE user_id = :userId");
        sqlParse.addSQL(" AND is_show = '1'");
        sqlParse.addSQL(" ORDER BY t.comment_id desc ");
        return CmfVideoComment.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Long add(Long userId, String content, Long videoId, Long replyCommentId, Long parentCommentId, String type, Long toUserId) {
        CmfVideoComment videoComment = new CmfVideoComment();
        videoComment.setUserId(userId);
        videoComment.setContent(content);
        videoComment.setVideoId(videoId);
        videoComment.setReplyCommentId(replyCommentId);
        videoComment.setParentCommentId(parentCommentId);
        videoComment.setType(type);
        videoComment.setReplys(0);
        videoComment.setLikes(0);
        videoComment.setToUserId(toUserId);
        videoComment.setCreateTime(new Date());
        videoComment.save();
        return videoComment.getCommentId();
    }

    @Override
    public Long add(Long userId, String content, Long videoId) {
        CmfVideoComment videoComment = new CmfVideoComment();
        videoComment.setUserId(userId);
        videoComment.setContent(content);
        videoComment.setVideoId(videoId);
        videoComment.setReplyCommentId(0L);
        videoComment.setParentCommentId(0L);
        videoComment.setType(CmfVideoComment.TYPE_COMMENT);
        videoComment.setReplys(0);
        videoComment.setLikes(0);
        videoComment.setCreateTime(new Date());
        videoComment.save();

        return videoComment.getCommentId();
    }

    @Override
    public void delete(Long commentId) {
        CmfVideoComment.dao.deleteById(commentId);
    }

    @Override
    public void incLikes(Long commentId) {
        String sql = "update cmf_video_comment set likes = likes+1 where comment_id = ?";
        Db.update(sql, commentId);
    }

    @Override
    public void incReplys(Long commentId) {
        String sql = "update cmf_video_comment set replys = replys+1 where comment_id = ?";
        Db.update(sql, commentId);
    }
}
